package com.hxx.mapper.impl;

import com.hxx.domain.TUser;
import com.hxx.mapper.UserMapper;
import com.hxx.utils.DbManager;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

public class UserMapperImpl implements UserMapper {

    private Connection conn = DbManager.getInstance().getConnection();
    private QueryRunner queryRunner = new QueryRunner();
    @Override
    public int insert(TUser user) {
        try {
            return queryRunner.update(conn,
                    "insert into t_user(username, sex, age) values(?, ?, ?)",
                    user.getUsername(), user.getSex(), user.getAge());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void update(TUser user) {
        try {
            queryRunner.update(conn,
                    "update t_user set username=?, sex=?, age=? where id=?",
                    user.getUsername(), user.getSex(), user.getAge(), user.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void delete(Integer id) {
        try {
            queryRunner.update(conn, "delete from t_user where id=?", id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public TUser selectById(Integer id) {
        try {
            return queryRunner.query(conn,
                    "select id, username, sex, age from t_user where id=?",
                    new BeanHandler<>(TUser.class), id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<TUser> selectAll() {
        try {
            return queryRunner.query(conn,
                    "select id, username, sex, age from t_user",
                    new BeanListHandler<>(TUser.class));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean login(String username, String password) {
        try {
            Integer count = queryRunner.query(conn,
                    "select count(*) from t_user where username=? and password=?",
                    new BeanHandler<>(Integer.class), username, password);
            if (count > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<TUser> findByPage(int pageNo, int pageSize) {
        List<TUser> list = new ArrayList<>();
        try {
            list = queryRunner.query(conn,
                    "select id, username, sex, age from t_user limit ?,?",
                    new BeanListHandler<>(TUser.class),pageNo,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int getTotalCount() {
        int count = 0;
        try {
            Object query = queryRunner.query(conn, "select count(*) from t_user",
                    new ScalarHandler<>());
            count = Integer.parseInt(query.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }
}
